home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / rend10.lzh / REND1.0 / GraphicSubSystem / matrix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  5.0 KB  |  150 lines

  1. /*
  2.  * Title:
  3.  *    matrix.c
  4.  *
  5.  * Authors:
  6.  *    Michael P. Schenck
  7.  *
  8.  * Purpose:
  9.  *    This module provides all the necessary matrix operations.  Allocating,
  10.  *    and deallocating.  Setting a matrix to identity.  Multiplying two
  11.  *    matricies and multiplying a vector times a matrix.  It also provides
  12.  *    a fast matrix copy function.  The code in this module was all 
  13.  *     programmed in a linear fashion.  If a NULL value is returned from
  14.  *    either of the allocation routines, the item could not be allocated.
  15.  *
  16.  * Copyright Info:
  17.  *    Copyright (C) 1993, 1994 -- by Michael P. Schenck, 
  18.  *    (mps4466@ultb.isc.rit.edu)
  19.  *    
  20.  *    This program is free software; you can redistribute it and/or modify
  21.  *    it under the terms of the GNU General Public License as published
  22.  *    by the Free Software Foundation; either version 2 of the License,
  23.  *    or (at your option) any later version.
  24.  *
  25.  *    This software is distributed in the hope that it will be useful, but
  26.  *    WITHOUT ANY WARRANTY; without even the implied warranty of
  27.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.  *    GNU General Public License for more details.
  29.  *
  30.  *      For a copy of the GNU General Public License
  31.  *    write to the Free Software Foundation, 675 Mass Ave,
  32.  *    Cambridge, MA  02139, USA.
  33.  *
  34.  */
  35.  
  36. #include <stdlib.h>
  37. #include "/include/types.h"
  38. #include "/include/matrix.h"
  39.  
  40.     /* Identity matrix. */
  41.  
  42. static FLOAT im[4*4] = {1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0};
  43.  
  44.     /* allocate and deallocate matricies and vectors */
  45.                     
  46. VECTOR allocatevector()
  47. {
  48.     return((VECTOR)malloc(4*sizeof(FLOAT)));
  49. }
  50.  
  51. MATRIX allocatematrix()
  52.  
  53. {
  54.     return((MATRIX)malloc(4*4*sizeof(FLOAT)));
  55. }
  56.  
  57. void freevector(VECTOR v)
  58.  
  59. {
  60.     free((void *)v);
  61. }
  62.     
  63. void freematrix(MATRIX m)
  64.  
  65. {
  66.     free((void *)m);
  67. }                
  68.                 
  69.     /* Copy matrix a to b. */
  70.          
  71. void copymatrix(MATRIX ma,MATRIX mb)
  72.  
  73. {   
  74.     *mb = *ma;
  75.        *(mb+1) = *(ma+1);
  76.     *(mb+2) = *(ma+2);
  77.     *(mb+3) = *(ma+3);
  78.     *(mb+4) = *(ma+4);
  79.     *(mb+5) = *(ma+5);
  80.     *(mb+6) = *(ma+6);
  81.     *(mb+7) = *(ma+7);
  82.     *(mb+8) = *(ma+8);
  83.     *(mb+9) = *(ma+9);
  84.     *(mb+10) = *(ma+10);
  85.     *(mb+11) = *(ma+11);
  86.     *(mb+12) = *(ma+12);
  87.     *(mb+13) = *(ma+13);
  88.     *(mb+14) = *(ma+14);
  89.     *(mb+15) = *(ma+15);
  90. }      
  91.                  
  92.     /* Set mr to the identity matrix. */
  93.             
  94. void identitymatrix(MATRIX mr)
  95.  
  96. {
  97.     *mr = *im;
  98.        *(mr+1) = *(im+1);
  99.     *(mr+2) = *(im+2);
  100.     *(mr+3) = *(im+3);
  101.     *(mr+4) = *(im+4);
  102.     *(mr+5) = *(im+5);
  103.     *(mr+6) = *(im+6);
  104.     *(mr+7) = *(im+7);
  105.     *(mr+8) = *(im+8);
  106.     *(mr+9) = *(im+9);
  107.     *(mr+10) = *(im+10);
  108.     *(mr+11) = *(im+11);
  109.     *(mr+12) = *(im+12);
  110.     *(mr+13) = *(im+13);
  111.     *(mr+14) = *(im+14);
  112.     *(mr+15) = *(im+15);
  113. }           
  114.  
  115.     /* Multiply ma x mb and store in mr. */
  116.  
  117. void multmatrix(MATRIX ma,MATRIX mb,MATRIX mr)
  118.  
  119. {
  120.     *mr = (*(ma))*(*(mb))+(*(ma+1))*(*(mb+4))+(*(ma+2))*(*(mb+4*2))+(*(ma+3))*(*(mb+4*3));
  121.     *(mr+1) = (*(ma))*(*(mb+1))+(*(ma+1))*(*(mb+4+1))+(*(ma+2))*(*(mb+4*2+1))+(*(ma+3))*(*(mb+4*3+1));
  122.     *(mr+2) = (*(ma))*(*(mb+2))+(*(ma+1))*(*(mb+4+2))+(*(ma+2))*(*(mb+4*2+2))+(*(ma+3))*(*(mb+4*3+2));
  123.     *(mr+3) = (*(ma))*(*(mb+3))+(*(ma+1))*(*(mb+4+3))+(*(ma+2))*(*(mb+4*2+3))+(*(ma+3))*(*(mb+4*3+3));
  124.     *(mr+4) = (*(ma+4))*(*(mb))+(*(ma+4+1))*(*(mb+4))+(*(ma+4+2))*(*(mb+4*2))+(*(ma+4+3))*(*(mb+4*3));
  125.     *(mr+4+1) = (*(ma+4))*(*(mb+1))+(*(ma+4+1))*(*(mb+4+1))+(*(ma+4+2))*(*(mb+4*2+1))+(*(ma+4+3))*(*(mb+4*3+1));
  126.     *(mr+4+2) = (*(ma+4))*(*(mb+2))+(*(ma+4+1))*(*(mb+4+2))+(*(ma+4+2))*(*(mb+4*2+2))+(*(ma+4+3))*(*(mb+4*3+2));
  127.     *(mr+4+3) = (*(ma+4))*(*(mb+3))+(*(ma+4+1))*(*(mb+4+3))+(*(ma+4+2))*(*(mb+4*2+3))+(*(ma+4+3))*(*(mb+4*3+3));
  128.        *(mr+2*4) = (*(ma+2*4))*(*(mb))+(*(ma+2*4+1))*(*(mb+4))+(*(ma+2*4+2))*(*(mb+4*2))+(*(ma+2*4+3))*(*(mb+4*3));
  129.     *(mr+2*4+1) = (*(ma+2*4))*(*(mb+1))+(*(ma+2*4+1))*(*(mb+4+1))+(*(ma+2*4+2))*(*(mb+4*2+1))+(*(ma+2*4+3))*(*(mb+4*3+1));
  130.     *(mr+2*4+2) = (*(ma+2*4))*(*(mb+2))+(*(ma+2*4+1))*(*(mb+4+2))+(*(ma+2*4+2))*(*(mb+4*2+2))+(*(ma+2*4+3))*(*(mb+4*3+2));
  131.     *(mr+2*4+3) = (*(ma+2*4))*(*(mb+3))+(*(ma+2*4+1))*(*(mb+4+3))+(*(ma+2*4+2))*(*(mb+4*2+3))+(*(ma+2*4+3))*(*(mb+4*3+3));
  132.      *(mr+3*4) = (*(ma+3*4))*(*(mb))+(*(ma+3*4+1))*(*(mb+4))+(*(ma+3*4+2))*(*(mb+4*2))+(*(ma+3*4+3))*(*(mb+4*3));
  133.     *(mr+3*4+1) = (*(ma+3*4))*(*(mb+1))+(*(ma+3*4+1))*(*(mb+4+1))+(*(ma+3*4+2))*(*(mb+4*2+1))+(*(ma+3*4+3))*(*(mb+4*3+1));
  134.     *(mr+3*4+2) = (*(ma+3*4))*(*(mb+2))+(*(ma+3*4+1))*(*(mb+4+2))+(*(ma+3*4+2))*(*(mb+4*2+2))+(*(ma+3*4+3))*(*(mb+4*3+2));
  135.     *(mr+3*4+3) = (*(ma+3*4))*(*(mb+3))+(*(ma+3*4+1))*(*(mb+4+3))+(*(ma+3*4+2))*(*(mb+4*2+3))+(*(ma+3*4+3))*(*(mb+4*3+3));
  136.  
  137.     /* Multiply v x m and store in r, this is the standard used in this code.
  138.        alot of computer graphics books multiply the m times v.  This is the
  139.        same as multiplying it this way, but with the transpose of the m. */
  140.  
  141. void multvecandmat(VECTOR v,MATRIX m,VECTOR r)
  142.  
  143. {  
  144.     *r = (*(v))*(*(m))+(*(v+1))*(*(m+4))+(*(v+2))*(*(m+2*4))+(*(v+3))*(*(m+3*4));
  145.     *(r+1) = (*(v))*(*(m+1))+(*(v+1))*(*(m+4+1))+(*(v+2))*(*(m+2*4+1))+(*(v+3))*(*(m+3*4+1));
  146.     *(r+2) = (*(v))*(*(m+2))+(*(v+1))*(*(m+4+2))+(*(v+2))*(*(m+2*4+2))+(*(v+3))*(*(m+3*4+2));
  147.     *(r+3) = (*(v))*(*(m+3))+(*(v+1))*(*(m+4+3))+(*(v+2))*(*(m+2*4+3))+(*(v+3))*(*(m+3*4+3));        
  148. }
  149.